//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/11.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import SnapKit

 @objc protocol  VisitorLoginViewDelegate: NSObjectProtocol {
    
   optional func userWillRegister()
    
   optional func userWillLogin()
    
    
    
}

class VisitorLoginView: UIView {
    
    weak var visitorViewDelegate : VisitorLoginViewDelegate?
    
    @objc private func loginBtnDidClick(){
        
        visitorViewDelegate?.userWillLogin?()
        
    }
    
    @objc private func registerBtnDidClick(){
        
        visitorViewDelegate?.userWillRegister?() 
    }
    
    
    func startAnimation(){
        
        let base  = CABasicAnimation(keyPath: "transform.rotation")
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        base.duration = 20
        base.removedOnCompletion = false
        circleIcon.layer.addAnimation(base, forKey: nil)
    }
    
    
    func setupUIWithInfo(imageName : String? , tipText :String){
        
        if let img = imageName{
            circleIcon.image = UIImage(named: img)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
            
        }else{
            
            startAnimation()
            
        }
        tipLabel.text = tipText
        
        
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI() 
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        addSubview(circleIcon)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint( NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0) )
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
            
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
            
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(80)
            make.height.equalTo(30)
        }

        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(80)
            make.height.equalTo(30)

        }
   
         backView.snp_makeConstraints { (make) -> Void in
            
          //  make.center.equalTo(circleIcon.snp_center)
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
            
        }
        
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var largeHouse : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    lazy var tipLabel : UILabel = UILabel.labelFactory("登录后别人就可以评论你的微博了登录后别人就可以评论你的微博了", fontSize: 14, color: UIColor.darkGrayColor())
    
    
//    {
//       
//        let l = UILabel()
//        
//        l.text = "登录后别人就可以评论你的微博了登录后别人就可以评论你的微博了"
//        l.font = UIFont.systemFontOfSize(14)
//        l.textColor = UIColor.darkGrayColor()
//        l.sizeToFit()
//        l.textAlignment = .Center
//        l.numberOfLines = 0
//        
//        return l
//        
//    }()
    
    lazy var loginBtn : UIButton = UIButton(title: "登录", fontSize: 16, color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
    
//    {
//        
//        let btn = UIButton()
//        
//        btn.setTitle("登录", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        
//        return btn
//    }()
    
    lazy var registerBtn : UIButton = UIButton(title: "注册", fontSize: 16, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
//    {
//        
//        let btn = UIButton()
//        
//        btn.setTitle("注册", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        
//        return btn
//        }()

    
    
}
